// pages/zstz/index/index.js
const app = getApp().globalData;
var md5 = require("../../../utils/md5.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    unitQuestion: [], //单位题库
    unitQuestions: [], //单位题库(免遍历)
    defaultQuestion: [], //默认题库
    hasAlreadySignUp: '', //是否已经报名
    formId: '', //报名表单id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getQuestionType(); //获取题库分类(单位题库与默认题库)
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    });
    wx.setNavigationBarTitle({ //动态修改标题
      title: '知识挑战'
    });
  },

  //获取题库分类(单位题库与默认题库)
  getQuestionType: function () {
    let that = this;
    var datetime = new Date().Format('yyyy-MM-dd hh:mm:ss');
    var token = md5.md5("[fid=" + app.fid + "][date=" + datetime + "]zs0dj4jc8dwtz").toLowerCase();
    wx.request({
      url: 'https://zstz.chaoxing.com/external/interface/wx/getQuestionType?token=' + token + '&fid=' + app.fid,
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            unitQuestion: res.data.data.unitQuestion,
            unitQuestions: res.data.data.unitQuestions,
            defaultQuestion: res.data.data.defaultQuestion
          })
          //先判断是否需要报名，再判断是否已经报名
          if (res.data.data.unitQuestion && res.data.data.unitQuestion[0].signupUrl) {
            let url = res.data.data.unitQuestion[0].signupUrl.split("?")[1];
            let formId = url.substring(3, url.indexOf('&'));
            that.hasAlreadySignUp(formId);
            that.setData({
              formId: formId
            })
          }
        }
      },
      fail: function () {
        wx.showToast({ //弹窗
          title: '获取题库异常！',
          icon: 'none'
        })
      }
    })
  },

  //判断是否已经报名
  hasAlreadySignUp: function (formId) {
    var that = this;
    var datetime = new Date().Format('yyyy-MM-dd hh:mm:ss');
    var token = md5.md5("[formId=" + formId + "][fid=" + app.fid + "][uid=" + wx.getStorageSync('cxid') + "][date=" + datetime + "]zs0dj4jc8dwtz").toLowerCase();
    wx.request({
      url: 'https://zstz.chaoxing.com/external/interface/wx/hasAlreadySignUp?formId=' + formId + '&fid=' + app.fid + '&uid=' + wx.getStorageSync('cxid') + '&token=' + token,
      success: function (res) {
        if (res.statusCode == 200) {
          that.setData({
            hasAlreadySignUp: res.data,
          })
        }
      },
      fail: function () {
        wx.showToast({ //弹窗
          title: '判断是否已经报名异常！',
          icon: 'none'
        })
      }
    })
  },

  //点击'轮播图'
  goUnitQuestion: function (event) {
    var that = this;
    let typeId = event.currentTarget.dataset.value.id;
    let unitQuestions = that.data.unitQuestions[typeId];
    //1.判断是否在活动时间内
    let date = new Date().Format('yyyy-MM-dd');
    if (unitQuestions.startTime && date < unitQuestions.startTime) {
      wx.showToast({
        title: '活动未开始！',
        icon: 'none'
      });
    } else if (unitQuestions.endTime && date > unitQuestions.endTime) {
      wx.showToast({
        title: '活动已结束！',
        icon: 'none'
      });
    } else {
      //2.判断是否已经报名
      if (unitQuestions.hasSignup == 1 && that.data.hasAlreadySignUp == false) {
        wx.showToast({
          title: '请先报名！',
          icon: 'none'
        });
      } else {
        //3.前去答题
        wx.navigateTo({
          url: '../activityCate/activityCate?typeId=' + typeId + '&typeName=' + event.currentTarget.dataset.value.typeName
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.hasAlreadySignUp(this.data.formId);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})